package com.lgx.authorize.module.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.stereotype.Component;

import com.lgx.authorize.module.dao.IApplicationApplyKeyInfoDao;
import com.lgx.authorize.module.entity.ApplicationApplykeyInfo;
import com.lgx.authorize.module.service.IApplicationApplykeyInfoService;
import com.lgx.authorize.util.DateCharger;
import com.lgx.authorize.util.RowMapperEffective;

/**
 * 
 * @author pengdeyi (king)
 *
 */
@Component("applicationApplykeyInfoDao")
public class ApplicationApplykeyInfoDao implements IApplicationApplyKeyInfoDao {

	@Autowired
	JdbcTemplate jdbcTemplate_sqlite;
	
	

	public JdbcTemplate getJdbcTemplate_sqlite() {
		return jdbcTemplate_sqlite;
	}

	@Override
	public void createApplicationApplyKeyInfo(
			final ApplicationApplykeyInfo applicationApplykeyInfo) {
		String sql = "insert into applicationapplykeyinfo" +
				"(appName,appkey,applyTime,disableTime,refershTime) " +
				"values(?,?,?,?,?)";
		getJdbcTemplate_sqlite().execute(sql, new PreparedStatementCallback<ApplicationApplykeyInfo>() {

			@Override
			public ApplicationApplykeyInfo doInPreparedStatement(
					PreparedStatement ps) throws SQLException,
					DataAccessException {
				ps.setString(1, applicationApplykeyInfo.getAppName());
				ps.setString(2, applicationApplykeyInfo.getAppkey());
				ps.setString(3, DateCharger.chargeTime(applicationApplykeyInfo.getApplyTime()));
				ps.setString(4, DateCharger.chargeTime(applicationApplykeyInfo.getDisableTime()));
				ps.setString(5, DateCharger.chargeTime(applicationApplykeyInfo.getRefershTime()));
				ps.execute();
				return applicationApplykeyInfo;
			}
		});
	}
	
	@Override
	public void updateApplicationApplykeyInfo(
			final ApplicationApplykeyInfo applicationApplykeyInfo) {
		String sql = "update applicationapplykeyinfo" +
				" set appName=?,appkey=?,applyTime=?,disableTime=?,refershTime=? " +
				" where systemid=?";
		getJdbcTemplate_sqlite().execute(sql, new PreparedStatementCallback<ApplicationApplykeyInfo>() {

			@Override
			public ApplicationApplykeyInfo doInPreparedStatement(
					PreparedStatement ps) throws SQLException,
					DataAccessException {
				ps.setString(1, applicationApplykeyInfo.getAppName());
				ps.setString(2, applicationApplykeyInfo.getAppkey());
				ps.setString(3, DateCharger.chargeTime(applicationApplykeyInfo.getApplyTime()));
				ps.setString(4, DateCharger.chargeTime(applicationApplykeyInfo.getDisableTime()));
				ps.setString(5, DateCharger.chargeTime(applicationApplykeyInfo.getRefershTime()));
				ps.setInt(6, applicationApplykeyInfo.getSystemid());
				ps.execute();
				return applicationApplykeyInfo;
			}
		});
	}

	@Override
	public void deleteApplicationApplykeyInfo(
			final ApplicationApplykeyInfo applicationApplykeyInfo) {
		String sql = "delete from applicationapplykeyinfo where systemid=?";
		getJdbcTemplate_sqlite().execute(sql, new PreparedStatementCallback<ApplicationApplykeyInfo>() {

			@Override
			public ApplicationApplykeyInfo doInPreparedStatement(
					PreparedStatement ps) throws SQLException,
					DataAccessException {
				ps.setInt(1, applicationApplykeyInfo.getSystemid());
				ps.execute();
				return applicationApplykeyInfo;
			}
		});
	}

	@Override
	public ApplicationApplykeyInfo findApplicationApplykeyInfoBySystemid(
			int systemid) {
		String sql = "select * from applicationapplykeyinfo where systemid=?";
		Object[] args = new Object[]{systemid};
		List<ApplicationApplykeyInfo> list = getJdbcTemplate_sqlite().query(sql,args, new RowMapperEffective<ApplicationApplykeyInfo>(ApplicationApplykeyInfo.class));
		return list==null||list.size()==0?null:list.get(0);
	}
	

	@Override
	public ApplicationApplykeyInfo findApplicationApplyKeyInfoByName(String name) {
		String sql = "select * from applicationapplykeyinfo where appName=?";
		Object[] args = new Object[]{name};
		List<ApplicationApplykeyInfo> list = getJdbcTemplate_sqlite().query(sql, args,new RowMapperEffective<ApplicationApplykeyInfo>(ApplicationApplykeyInfo.class));
		return list==null||list.size()==0?null:list.get(0);
	}

	@Override
	public ApplicationApplykeyInfo findApplicationApplykeyInfoByNameKey(
			String appName, String key) {
		String sql = "select * from applicationapplykeyinfo where appName=? and appkey=?";
		Object[] args = new Object[]{appName,key};
		List<ApplicationApplykeyInfo> list = getJdbcTemplate_sqlite().query(sql,args, new RowMapperEffective<ApplicationApplykeyInfo>(ApplicationApplykeyInfo.class));
		return list==null||list.size()==0?null:list.get(0);
	}

	@Override
	public ApplicationApplykeyInfo findApplicationApplykeyInfoByNamekeyTime(
			String appName, String key, Date Time) {
		String sql = "select * from applicationapplykeyinfo where appName=? and appkey=? and apptime<=?";
		Object[] args = new Object[]{appName,key,DateCharger.chargeTime(Time)};
		List<ApplicationApplykeyInfo> list = getJdbcTemplate_sqlite().
				query(sql, args,new RowMapperEffective<ApplicationApplykeyInfo>(ApplicationApplykeyInfo.class));
		return list==null?null:list.get(0);
	}

	@Override
	public List<ApplicationApplykeyInfo> listAllApplicationApplykeyInfos(){
		String sql = "select * from applicationapplykeyinfo where  disableTime<=?";
		Object[] args = new Object[]{DateCharger.chargeTime(new Date())};
		return getJdbcTemplate_sqlite().query(sql, args,new RowMapperEffective<ApplicationApplykeyInfo>(ApplicationApplykeyInfo.class));
	}

	@Override
	public List<ApplicationApplykeyInfo> listApplicationApplykeyInfosByDate(
			Date startTime, Date endTime, String timeType) {
		String sql = "select select * from applicationapplykeyinfo";
		if(timeType.equals(IApplicationApplykeyInfoService.APPLICATION_TIME_TYPE_APPLYTIME)){
			sql += " where applyTime>=? and applyTime<=?";
		}else if(timeType.equals(IApplicationApplykeyInfoService.APPLICATION_TIME_TYPE_DISABLETIME)){
			sql += " where disableTime>=? and applyTime<=?";
		}else if(timeType.equals(IApplicationApplykeyInfoService.APPLICATION_TIME_TYPE_REFRESHTIME)){
			sql += " where refershTime>=? and refershTime<=?";
		}
		Object[] args = new Object[]{DateCharger.chargeTime(startTime),DateCharger.chargeTime(endTime)};
		return getJdbcTemplate_sqlite().query(sql, args,new RowMapperEffective<ApplicationApplykeyInfo>(ApplicationApplykeyInfo.class));
	}

}
